package cn.doctor.assistant.pad;

import android.app.Activity;

import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMMessage;

import java.util.ArrayList;
import java.util.List;

import cn.doctor.assistant.pad.entity.AssUser;
import cn.doctor.assistant.pad.im.IMApplication;

/**
 * Created by jiaolf on 2016/2/22.
 */
public class MyApplication extends IMApplication {

    private static MyApplication instance;
    private Activity mCurrActivity;

    public static MyApplication getInstance() {
        return instance;
    }

    public Activity getCurrentActivity() {
        return this.mCurrActivity;
    }

    // 当前登录的助理用户
    public AssUser mAssistUser;

    // 助理对应的医生列表
    public List<AssUser> mDoctors;

    // 当前选择的医生
    public AssUser mCurrDoctor;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public void enter(Activity activity) {
        this.mCurrActivity = activity;
    }

    //---------------- 助理当前选择的医生 -----------------------------
    // 助理可能有多个医生，如果切换当前的医生，可以在这里做监听
    public interface IDoctorChangedListener{
        void updateDoctor(AssUser doctor);
    }

    private List<IDoctorChangedListener> mDoctorChangedListeners = new ArrayList<>();

    public void addDoctorChangedListener(IDoctorChangedListener listener) {
        if (!mDoctorChangedListeners.contains(listener)) {
            mDoctorChangedListeners.add(listener);
        }
    }

    // TODO 如果切换医生，则需要重新获取医生对于的好友列表
    public void switchCurrentDoctor(int position) {
        mCurrDoctor = mDoctors.get(position);
        for (IDoctorChangedListener listener : mDoctorChangedListeners) {
            listener.updateDoctor(mCurrDoctor);
        }
    }

    // ------------------- 聊天消息 -------------------------
    // 监听环信的聊天消息
    public interface MessageEventListener {
        void refrush(EMNotifierEvent.Event eventType, EMMessage message);
    }

    private List<MessageEventListener> mMsgEventListener = new ArrayList<>();

    public void addMsgEventListener(MessageEventListener listener) {
        if (!mMsgEventListener.contains(listener)) {
            mMsgEventListener.add(listener);
        }
    }

    public void removeMsgEventListener(MessageEventListener listener) {
        if (mMsgEventListener.contains(listener)) {
            mMsgEventListener.remove(listener);
        }
    }

    public void notifyNewMessage(EMNotifierEvent.Event eventType, EMMessage message) {
        for (MessageEventListener listener : mMsgEventListener) {
            if (listener != null) {
                listener.refrush(eventType, message);
            }
        }
    }

}
